package Tests;

import static org.junit.Assert.*;

import java.security.InvalidKeyException;
import java.security.spec.InvalidKeySpecException;

import org.junit.Test;

import Email.auto.checker.DESEncryption;

public class TestDESEncryption {

	private static String encryptKey = null;
	
	@Test
	public void testCreateDESEncryptionValidKey() {
		encryptKey = "umachave";
		DESEncryption des;
		try {
			
			des = new DESEncryption(encryptKey);
			assertNotNull(des);
			
		} catch (InvalidKeyException e) {
			e.printStackTrace();
		} catch (InvalidKeySpecException e) {
			e.printStackTrace();
		}
	}
	
	@Test(expected = NullPointerException.class)
	public void testCreateDESEncryptionNullKey() throws InvalidKeyException, InvalidKeySpecException {
		encryptKey = null;
		DESEncryption des = new DESEncryption(encryptKey);
	}
	
	@Test(expected = InvalidKeyException.class)
	public void testCreateDESEncryptionShorterKey() throws InvalidKeyException, InvalidKeySpecException {
		encryptKey = "1234567";
		DESEncryption des = new DESEncryption(encryptKey);
	}
	
	@Test
	public void testEncryptString(){
		encryptKey = "umachave";
		try {

			DESEncryption des = new DESEncryption(encryptKey);
			assertNotNull(des.encrypt("SerEncrsdfsSasd"));
			
		} catch (InvalidKeyException e) {
			e.printStackTrace();
		} catch (InvalidKeySpecException e) {
			e.printStackTrace();
		}
	}
	
	@Test
	public void testEncryptStringNull(){
		encryptKey = "umachave";
		try {

			DESEncryption des = new DESEncryption(encryptKey);
			assertNull(des.encrypt(null));
			
		} catch (InvalidKeyException e) {
			e.printStackTrace();
		} catch (InvalidKeySpecException e) {
			e.printStackTrace();
		}
	}
	
	@Test
	public void testEncryptStringEmpty(){
		encryptKey = "umachave";
		try {

			DESEncryption des = new DESEncryption(encryptKey);
			assertNull(des.encrypt(""));
			
		} catch (InvalidKeyException e) {
			e.printStackTrace();
		} catch (InvalidKeySpecException e) {
			e.printStackTrace();
		}
	}
	
	@Test
	public void testDecryptByteArray(){
		encryptKey = "umachave";
		try {

			DESEncryption des = new DESEncryption(encryptKey);
			assertNull(des.desencrypt("umteste".getBytes()));
			
		} catch (InvalidKeyException e) {
			e.printStackTrace();
		} catch (InvalidKeySpecException e) {
			e.printStackTrace();
		}
	}
	
	@Test
	public void testDecryptByteArrayNull(){
		encryptKey = "umachave";
		try {

			DESEncryption des = new DESEncryption(encryptKey);
			assertNull(des.desencrypt(null));
			
		} catch (InvalidKeyException e) {
			e.printStackTrace();
		} catch (InvalidKeySpecException e) {
			e.printStackTrace();
		}
	}

}
